/**
 * 
 */
package com.cqcis.uip.module.salary.oper.handlers;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.cqcis.uip.base.framework.common.Task;
import com.cqcis.uip.base.framework.core.RegisteredServiceContext;
import com.cqcis.uip.base.framework.core.Service;
import com.cqcis.uip.base.framework.handlers.TableDispatchService;
import com.cqcis.uip.base.framework.utils.LoopThreadPoolExecutor;
import com.cqcis.uip.module.salary.oper.service.ProcessService;


/**
 * 
 * @author 李世军
 *
 * 创建于 2012-3-7 下午01:06:42
 */
public class SalaryHandler  implements TableDispatchService, Service {
	private static Log log = LogFactory.getLog(SalaryHandler.class);
	private LoopThreadPoolExecutor salaryExecutor; // 线程池
	private long intervalTime=2000; // 出错后线程等待时间
	private int getThreadNumber=1; // 数据线程数
	private Object lockGet = new Object(); // lock
	private ProcessService processService ;
	
	public void setProcessService(ProcessService processService) {
		this.processService = processService;
	}

	public void start(RegisteredServiceContext serviceContext) throws Exception {
//		int threadNumber = getThreadNumber;
//		salaryExecutor = LoopThreadPoolExecutor.getExecutor(threadNumber,
//				threadNumber * 2, 60, serviceContext.getLogService(),
//				serviceContext.getRegisteredService().getLogConfiguration(),
//				serviceContext.getRegisteredService().getName(), intervalTime);
//
//		for (int i = 0; i < getThreadNumber; i++) {
//			salaryExecutor.execute(new Runnable() {
//				public void run() {
//					try {
						process() ;
//					} catch (Throwable t) {
//						log.info("Catch Throwable in process salary.", t);
//					}
//				}
//			});
//		}
	}
	
	public void stop(RegisteredServiceContext serviceContext) throws Exception {
		if (salaryExecutor != null) {
			salaryExecutor.shutdown();
		}
	}
	
	private void process(){
//		while(true) {
//			synchronized (lockGet) {
//				try {
//					Thread.sleep(30000) ;
//				} catch (Exception e) {
//					log.info("thread sleep exception:" + e.toString()) ;
//				}
				
				processService.finishContract() ;
				processService.doProcess() ;
//			}
//		}
	}
	
	public List<Task> get(String interfaceId) throws Exception {
		// do nothing
		log.info("[get]") ;
		return null;
	}
	
	public boolean processResult(List<Task> tasks) {
		// do nothing
		log.info("[processResult]") ;
		return false;
	}
	
	public boolean schedule(Task task) throws Exception {
		// do nothing
		return false;
	}
	
}
